Fk:loadTranslationTable{
  ["dajubei__anmou"] = "暗谋",
  [":dajubei__anmou"] = "一名角色的出牌阶段开始时，你可以扣置一张手牌。若如此做，其回合结束时，你可将扣置的牌当作该角色出牌阶段使用的第一张即时牌使用，或弃置扣置的牌。",

  ["$dajubei__anmou_pile"] = "暗谋",
  ["#dajubei__anmou-put"] = "暗谋：可以扣置一张手牌，回合结束时，可将之当此阶段当前回合角色使用的第一张即时牌使用或弃置"
}

local anmou = fk.CreateSkill{
  name = "dajubei__anmou",
}

--- 是否是即时牌
---@param card Card
local function isImmediate(card)
  return card:isCommonTrick() or card.type == Card.TypeBasic
end

--- 出牌阶段第一张即时牌
---@param room Room
---@return Card?
local function findImmediate(room)
  local card
  local target = room.current
  local event = room.logic:getCurrentEvent()
  local accept
  local start_event = event:findParent(GameEvent.Turn, true)
  if not start_event then return card end
  for i = start_event.id, event.id do
    local e = room.logic.all_game_events[i]
    if e.event == GameEvent.Phase then
      local data = e.data ---@type PhaseData
      if data.phase == Player.Play and data.who == target then
        accept = true
      end
    end
    if accept and e.event == GameEvent.UseCard then
      local data = e.data ---@type UseCardData
      if data.from == target and isImmediate(data.card) then
        card = data.card
        break
      end
    end
  end
  return card
end

--- main
anmou:addEffect(fk.EventPhaseStart, {
  anim_type = "control",
  can_trigger = function (self, event, target, player, data)
    if player:hasSkill("dajubei__anmou") then
      return data.phase == Player.Play and not player:isKongcheng()
    end
  end,
  on_cost = function (self, event, target, player, data)
    local room = player.room
    local cards = room:askToCards(player, {
      cancelable = true,
      min_num = 1,
      max_num = 1,
      pattern = ".|.|.|hand",
      skill_name = "dajubei__anmou",
      prompt = "#dajubei__anmou-put",
    })
    if #cards ~= 1 then return false end
    event:setCostData(self, { cards = cards })
    return true
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local cards = event:getCostData(self).cards
    if not cards or player.dead then return end
    room:moveCardTo(cards, Card.PlayerSpecial, player, fk.ReasonPut, "dajubei__anmou", "$dajubei__anmou_pile", false, player)
  end
})

--- delay
anmou:addEffect(fk.TurnEnd, {
  is_delay_effect = true,
  anim_type = "special",
  can_trigger = function (self, event, target, player, data)
    if player:hasSkill("dajubei__anmou") then
      return #player:getPile("$dajubei__anmou_pile") > 0
    end
  end,
  on_cost = Util.TrueFunc,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local card = findImmediate(room)
    local function discard()
      room:moveCardTo(player:getPile("$dajubei__anmou_pile"), Card.DiscardPile, nil, fk.ReasonDiscard, "dajubei__anmou", nil, true, player)
    end
    if not card then
      discard()
    else
      local _, ret = room:askToUseActiveSkill(player, {
        skill_name = "dajubei__anmou_active",
        cancelable = false,
        extra_data = { cardName = card.name },
        prompt = "#dajubei__anmou_active:::" .. Fk:translate(card.name),
        skip = true,
      })
      if not ret then discard() return end
      if ret.interaction == "discard" then
        discard()
      else
        local _card = Fk:cloneCard(ret.interaction)
        _card:addSubcards(ret.cards)
        _card.skillName = "dajubei__anmou"
        room:useCard{
          card = _card,
          from = player,
          tos = ret.targets,
          extraUse = true,
        }
      end
    end
  end
})

return anmou